package SystemB;

/***********************************
* File:Security Console.java
* Course: 17655
* Project: Assignment 3
* Copyright: Copyright (c) 2013 Carnegie Mellon University
* Versions:
*	1.0 March 2013 - luisrocha
*
* Description: This class is the security console for the museum environmental control system
* expanded with the fire alarm and sprinkler functionality.

* Parameters: None
*
* Internal Methods: None
******************************************************************************************************************/
import EventPackage.*;
import TermioPackage.*;

class SecurityConsole
{
	public static void main(String args[])
	{
                Termio UserInput = new Termio();                // Termio IO Object
		boolean Done = false;				// Main loop flag
		String Option = null;				// Menu choice from user
		Event Evt = null;					// Event object
		boolean Error = false;				// Error flag
		SecurityMonitor Monitor = null;			// The environmental control system monitor
                int Mode = 0;                                   // Mode = 0 - security; Mode = 1 - fire alert; Mode = 2 - turn off sprinkler
                
                
		/////////////////////////////////////////////////////////////////////////////////
		// Get the IP address of the event manager
		/////////////////////////////////////////////////////////////////////////////////

 		if ( args.length != 0 )
 		{
			// event manager is not on the local system

			Monitor = new SecurityMonitor( args[0] );

		} else {

			Monitor = new SecurityMonitor();

		} // if


		// Here we check to see if registration worked. If ef is null then the
		// event manager interface was not properly created.

		if (Monitor.IsRegistered() )
		{
			Monitor.start(); // Here we start the monitoring and control thread

			while (!Done)
			{
                            // Here, the main thread continues and provides the main menu

                            System.out.println( "\n\n\n\n" );
                            System.out.println( "Security  Control System (ECS) Command Console: \n" );

                            if (args.length != 0)
				System.out.println( "Using event manager at: " + args[0] + "\n" );
                            else
				System.out.println( "Using local event manager \n" );

                         
                            // the Mode allows to differentiate between the states of the console
                            // in the normal mode, the console behaves as the security console from system A
                            // when an fire alarm event is detected, the console moves to mode 1 that allows
                            // to enable or cancel the sprinklers
                            // after sprinklers are turned on, the console moves to mode 2 to enable disabling
                            // the sprinklers
                            Mode = Monitor.getMode();                          
                            switch (Mode)
                            {
                            case 0: // security console mode
                           
				System.out.println( "Select an Option: \n" );
				System.out.println( "1: ARM Security System" );
				System.out.println( "2: DISARM Security System" );
                                System.out.println( "3: Generate smoke detector event for test purposes" );
				System.out.println( "X: Stop System\n" );
				System.out.print( "\n>>>> " );
				Option = UserInput.KeyboardReadString();

				//////////// option 1 ////////////
				if ( Option.equals( "1" ) )
				{
					// turn on the security system. Note that it is armed when initialized
					Monitor.ArmSystem();
                                        
				}
				//////////// option 2 ////////////
				if ( Option.equals( "2" ) )
				{
					// turn on the security system. Note that it is armed when initialized
					Monitor.DisarmSystem();
				}
                                
                                //////////// option 3 ////////////
				if ( Option.equals( "3" ) )
				{
					// generate smoke event
					Monitor.Sprinkler(false);
                                        Monitor.FireAlarm(false);
                                        Monitor.PostSmoke(true);
				}
				//////////// option X ////////////

				if ( Option.equalsIgnoreCase( "X" ) )
				{
					// Here the user is done, so we set the Done flag and halt
					// the environmental control system. The monitor provides a method
					// to do this. Its important to have processes release their queues
					// with the event manager. If these queues are not released these
					// become dead queues and they collect events and will eventually
					// cause problems for the event manager.

					Monitor.Halt();
					Done = true;
					System.out.println( "\nConsole Stopped... Exit monitor mindow to return to command prompt." );				
				} // if
                                break;
                            
                            case 1: // fire alert mode  
                                   
                                System.out.println( "FIRE ALERT - Select an Option: \n" );
				System.out.println( "1: Turn on Sprinkler (will turn on automatically after 15s)" );
				System.out.println( "2: Cancel Sprinkler" );
				System.out.println( "X: Stop System\n" );
				System.out.print( "\n>>>> " );
				Option = UserInput.KeyboardReadString();

				//////////// option 1 ////////////
				if ( Option.equals( "1" ) )
				{
					Monitor.Sprinkler(true);
				}
				//////////// option 2 ////////////
				if ( Option.equals( "2" ) )
				{
					//turn off sprinkler and fire alarm
                                        Monitor.Sprinkler(false);
                                        Monitor.FireAlarm(false);                                       
                                                                           
				}

				//////////// option X ////////////

				if ( Option.equalsIgnoreCase( "X" ) )
				{
					// Here the user is done, so we set the Done flag and halt
					// the environmental control system. The monitor provides a method
					// to do this. Its important to have processes release their queues
					// with the event manager. If these queues are not released these
					// become dead queues and they collect events and will eventually
					// cause problems for the event manager.

					Monitor.Halt();
					Done = true;
					System.out.println( "\nConsole Stopped... Exit monitor mindow to return to command prompt." );				
				} // if
                                break;
                                  
                            case 2: // sprinkler on mode
               
                                System.out.println( "SPRINKLERS ACTIVE - Select an Option: \n" );
				System.out.println( "1: Turn off Sprinkler" );
				System.out.println( "X: Stop System\n" );
				System.out.print( "\n>>>> " );
				Option = UserInput.KeyboardReadString();

				//////////// option 1 ////////////
				if ( Option.equals( "1" ) )
				{
					//turn off the sprinkler
                                        Monitor.Sprinkler(false);                                    
				}

				//////////// option X ////////////

				if ( Option.equalsIgnoreCase( "X" ) )
				{
					// Here the user is done, so we set the Done flag and halt
					// the environmental control system. The monitor provides a method
					// to do this. Its important to have processes release their queues
					// with the event manager. If these queues are not released these
					// become dead queues and they collect events and will eventually
					// cause problems for the event manager.

					Monitor.Halt();
					Done = true;
					System.out.println( "\nConsole Stopped... Exit monitor window to return to command prompt." );				
				} // if
                                break;
                            } //switch   
                                  
			} // while

		} else {

			System.out.println("\n\nUnable start the monitor.\n\n" );

		} // if

  	} // main

} // SecurityConsole
